package com.common.util.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.common.Cache;

/**
 * 数据库工具类
 * 
 * @author 13466816279@139.com
 */
public class DBUtil {
    /**
     * 数据库连接信息配置文件路径
     */
    private String path = getClass().getResource("/").getFile().toString() + "jdbc.properties";
    /**
     * 数据库连接
     */
    private Connection conn = null;

    /**
     * 获得数据库连接
     * 
     * @return
     */
    public Connection getConnection() {
        // InputStream in = null;
        // Properties prop = null;
        try {
            // in = new BufferedInputStream(new FileInputStream(path));
            // prop = new Properties();
            // prop.load(in);
            String driver = Cache.driver;
            String url = Cache.url;
            String username = Cache.username;
            String password = Cache.password;
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
            if (!conn.isClosed()) {
                System.out.println("Succeeded connecting to the Database!");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

}
